//
//  RGWithdrawCashViewController.m
//  RedGrass
//
//  Created by wukong on 2019/2/22.
//  Copyright © 2019年 hongcaosp. All rights reserved.
//

#import "RGWithdrawCashViewController.h"
#import "HttpRequest.h"


@interface RGWithdrawCashViewController ()

@property (strong, nonatomic) UILabel *alertLabel;
@property (strong, nonatomic) UITextField *cashTF;
@property (strong, nonatomic) UIView *backGroundView;
@property (strong, nonatomic) UILabel *cashCountLabel;
@property (strong, nonatomic) UIButton *cashAllBtn;
@property (strong, nonatomic) UIButton *nextBtn;
@property (strong, nonatomic) UILabel *alertLabel1;

@end

@implementation RGWithdrawCashViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showBackButton];
    self.NavStyleType = RGCustomNavStyleType_black_noLine;
    self.customNavBar.title = @"提现";
    [self initUI];
    [self updateMoneyText];
    _nextBtn.backgroundColor = UICOLOR_WITH_RGBINT(0x999999);
    _nextBtn.enabled = NO;
}

-(void) viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [_cashTF becomeFirstResponder];
}

-(void)updateMoneyText{
    if([_cash length] == 0){
        _cash = @"0";
    }
    NSString *tempStr = [NSString stringWithFormat:@"余额%@",_cash];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:tempStr];
    NSRange range = [tempStr rangeOfString:@"余额"];
    NSDictionary *dic = @{NSForegroundColorAttributeName : [UIColor whiteColor]};
    if(range.length){
        [attr addAttributes:dic range:range];
    }
    _cashCountLabel.attributedText = attr;
}

#pragma mark - UI

-(void)initUI{
    [self.customContentView addSubview:self.backGroundView];
    [_backGroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.mas_equalTo(self.customContentView).mas_offset(0);
        make.top.mas_equalTo(self.customContentView).mas_offset(40);
        make.height.mas_equalTo(44);
        
    }];
    
    [_backGroundView addSubview:self.alertLabel];
    [_alertLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backGroundView).mas_offset(0);
        make.left.mas_equalTo(self.backGroundView).mas_offset(15);
        make.width.mas_equalTo(80);
    }];
    
    [_backGroundView addSubview:self.cashTF];
    [_cashTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backGroundView).mas_offset(0);
        make.left.mas_equalTo(self.alertLabel.mas_right).mas_offset(4);
        make.right.mas_equalTo(self.backGroundView).mas_offset(8);
        make.height.mas_equalTo(44);
    }];
    
    [self.customContentView addSubview:self.cashCountLabel];
    [_cashCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.customContentView).mas_offset(15);
        make.top.mas_equalTo(self.backGroundView.mas_bottom).mas_offset(8);
    }];
    
    [self.customContentView addSubview:self.cashAllBtn];
    [_cashAllBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.cashCountLabel.mas_right).mas_offset(15);
        make.centerY.mas_equalTo(self.cashCountLabel.mas_centerY).mas_offset(0);
    }];
    
    [self.customContentView addSubview:self.nextBtn];
    [_nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.customContentView).mas_offset(15);
        make.right.mas_equalTo(self.customContentView).mas_offset(-15);
        make.height.mas_equalTo(46);
        make.top.mas_equalTo(self.cashCountLabel.mas_bottom).mas_offset(30);
    }];
    
    [self.customContentView addSubview:self.alertLabel1];
    [_alertLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.customContentView).mas_offset(0);
        make.top.mas_equalTo(self.nextBtn.mas_bottom).mas_offset(20);
    }];
}

#pragma mark -interface

-(void) WithdrawCash{
    
   NSDecimalNumber *cash = [NSDecimalNumber decimalNumberWithString:_cashTF.text];
    if(cash){
        float tempCash = [cash doubleValue];
        if(tempCash < 5){
            [self showTextMessageWithMessage:@"单笔提现金额不能少于5元"];
            return;
        }
        HttpRequest *quest = [HttpRequest buildRequest:@"/api/wallet/cashWithdrawal"];
        RGUserInfoModel *user = [RGUserInfoModel getUserInfo];
        NSDictionary *dic = @{@"token":SafeStr(user.token),
                              @"amount":@(tempCash),
                              @"type":@(1)
                              };
        [self showLoading:@""];
        AA_WEAK_MAKE(self) weakSelf = self;
        [quest post:dic success:^(id data) {
            [weakSelf dismissLoading];
            if(weakSelf.needUpdateBlock){
                weakSelf.needUpdateBlock();
            }
            [weakSelf showTextMessageWithMessage:@"提现成功,请等待工作人员审核"];
            [weakSelf performSelector:@selector(popVc) withObject:nil afterDelay:1];
        } fail:^(NSInteger code, NSString *message) {
            [weakSelf showTextMessageWithMessage:message];
        }];
    }
}

-(void)popVc{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - reponse

-(void)textFieldChanged:(UITextField *)textField{
    if([textField.text length] == 0){
        _nextBtn.enabled = NO;
        _nextBtn.backgroundColor = UICOLOR_WITH_RGBINT(0x999999);
    }else{

        NSDecimalNumber *currentCount = [NSDecimalNumber decimalNumberWithString:_cashTF.text];
        if(currentCount){
            _nextBtn.enabled = YES;
            _nextBtn.backgroundColor = BaseColor;
        }else{
            _nextBtn.enabled = NO;
            _nextBtn.backgroundColor = UICOLOR_WITH_RGBINT(0x999999);
        }
    }
}

-(void)cashAllBtnClick:(UIButton *)button{
   
    _cashTF.text = _cash;
    if([_cash length]){
        _nextBtn.enabled = YES;
        _nextBtn.backgroundColor = BaseColor;
    }else{
        _nextBtn.enabled = NO;
        _nextBtn.backgroundColor = UICOLOR_WITH_RGBINT(0x999999);
    }
 
   
}

-(void)nextBtnClick:(UIButton *)button{
    NSDecimalNumber *currentCount = [NSDecimalNumber decimalNumberWithString:_cashTF.text];
    if(currentCount){
        [self WithdrawCash];
    }else{
        [self showTextMessageWithMessage:@"请输入正确的金额"];
    }
}

#pragma mark - getter

-(UIView *)backGroundView{
    if(!_backGroundView){
        _backGroundView = [[UIView alloc]init];
        _backGroundView.backgroundColor = UICOLOR_WITH_RGBINT(0x483D8B);
    }
    return _backGroundView;
}

-(UILabel *)alertLabel{
    if(!_alertLabel){
        _alertLabel = [[UILabel alloc]init];
        _alertLabel.text = @"提现金额";
        _alertLabel.textColor = [UIColor whiteColor];
        _alertLabel.font = FONT(customFontName, 15);
    }
    return _alertLabel;
}

-(UITextField *)cashTF{
    if(!_cashTF){
        _cashTF = [[UITextField alloc]init];
        _cashTF.textColor = [UIColor whiteColor];
        _cashTF.font = FONT(customFontName, 15);
        _cashTF.placeholder = @"0.00";
        _cashTF.tintColor = BaseColor;
        _cashTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _cashTF.keyboardType = UIKeyboardTypeDecimalPad;
        [_cashTF addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _cashTF;
}

-(UILabel *)cashCountLabel{
    if(!_cashCountLabel){
        _cashCountLabel = [[UILabel alloc]init];
        _cashCountLabel.textColor = UICOLOR_WITH_RGBINT(0xFFD700);
        _cashCountLabel.font = FONT(customFontName, 15);
        _cashCountLabel.text = @"余额";
    }
    return _cashCountLabel;
}

-(UILabel *)alertLabel1{
    if(!_alertLabel1){
        _alertLabel1 = [[UILabel alloc]init];
        _alertLabel1.textColor = [UIColor whiteColor];
        _alertLabel1.font = FONT(customFontName, 15);
        _alertLabel1.text = @"预计明天24点前到账";
    }
    return _alertLabel1;
}

-(UIButton *)cashAllBtn{
    if(!_cashAllBtn){
        _cashAllBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cashAllBtn setTitleColor:BaseColor forState:UIControlStateNormal];
        [_cashAllBtn setTitle:@"全部提现" forState:UIControlStateNormal];
        [_cashAllBtn.titleLabel setFont:FONT(customFontName, 15)];
        [_cashAllBtn addTarget:self action:@selector(cashAllBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cashAllBtn;
}

-(UIButton *)nextBtn{
    if(!_nextBtn){
        _nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
        _nextBtn.backgroundColor = BaseColor;
        [_nextBtn.titleLabel setFont:FONT(customFontName, 16)];
        [_nextBtn addTarget:self action:@selector(nextBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _nextBtn;
}

@end
